<template>
  <div class="head">
    <div>
      <img v-if="none" src="https://khh-server.oss-cn-beijing.aliyuncs.com/resourse/IMG/static/Loading.gif" style="width:4rem;height:4rem;position: fixed;top:25%;left:23%;">
    </div>
  </div>
</template>

<script>
import config from "../config/config.js";
import store from "../store/newStore.js";
export default {
    store,
    data() {
        return {
            none: true
        };
    },
    computed: {
        ticket() {
            return this.$store.state.getTicket;
        }
    },
    activated() {
        var wxcode = localStorage.getItem("code");
        // var state = sessionStorage.getItem("path");
        // alert(state);
        var wxToken = localStorage.getItem("token");
        var value = JSON.stringify({ code: wxcode, type: "1" }),
            key = demo(value);
        var that = this;
        wx.miniProgram.getEnv(function(res) {
            localStorage.setItem("type", res.miniprogram);
        });
        if (window.history.length == 1) {
            localStorage.setItem("tag", 1);
        } else if (window.history.length == 3) {
            localStorage.setItem("tag", 2);
        }
        var tag = localStorage.getItem("tag");
        if (tag == 1) {
            //直接进入  在历史记录 >1 的情况下  关闭微信浏览器
            if (window.history.length > 1) {
                setTimeout(function() {
                    WeixinJSBridge.call("closeWindow");
                }, 0);
            }
        } else {
            //第三方界面进入  在历史记录 >3 的情况下  返回到-2 界面
            if (window.history.length > 3) {
                setTimeout(function() {
                    window.history.go(-3);
                }, 0);
            }
        }
        // localStorage.setItem('token','');
        if (wxToken === "" || wxToken === null) {
            axios
                .post(
                    "/Home/login/login",
                    {
                        key: key,
                        value: value,
                        ticket: this.ticket
                    },
                    config
                )
                .then(res => {
                    if (res.data.code == 200) {
                        localStorage.setItem("token", res.data.data.token);
                        this.$store.commit("increment", res.data.ticket);
                        if (state == "") {
                            that.$router.push("/home/index/00");
                        } else {
                            that.$router.push(state);
                        }
                    }
                });
        } else {
            // if(state==''){
            //   that.$router.push("/home/index/00");
            // }else{
            //   that.$router.push(state);
            // }
            axios
                .post(
                    "/Home/login/login",
                    {
                        key: key,
                        value: value,
                        ticket: this.ticket
                    },
                    config
                )
                .then(res => {
                    if (res.data.code == 200) {
                        localStorage.setItem("token", res.data.data.token);
                        this.$store.commit("increment", res.data.ticket);
                        if (state == "") {
                            that.$router.push("/home/index/00");
                        } else {
                            that.$router.push(state);
                        }
                    }
                });
        }
    },
    deactivated() {
        this.$destroy(true);
    }
};
</script>

<style lang="scss">
</style>